package com.itheima.reggie.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.entity.AddressBook;
import com.itheima.reggie.entity.OrderDetail;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.mapper.OrdersMapper;
import com.itheima.reggie.mapper.ShoppingCartMapper;
import com.itheima.reggie.service.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {

    @Autowired
    private AddressBookService addressBookService;
    @Autowired
    private UserService userService;
    @Autowired
    private ShoppingCartService shoppingCartService;
    @Autowired
    private OrderDetailService orderDetailService;


    @Override
    public void submit(Orders orders) {
        //地址簿信息
        Long addressBookId = orders.getAddressBookId();
        //订单号
        long id = IdWorker.getId();
        orders.setNumber(id + "");
        //下单用户id
        Long userId = BaseContext.getCurrentId();
        orders.setUserId(userId);
        //下单及结账日期
        LocalDateTime now = LocalDateTime.now();
        orders.setOrderTime(now);
        orders.setCheckoutTime(now);
        //收货人个人信息  //根据地址簿id查询出收货人信息
        AddressBook addressBook = addressBookService.getById(addressBookId);
        orders.setPhone(addressBook.getPhone());
        orders.setAddress(addressBook.getDetail());
        orders.setUserName(userService.getById(userId).getName());
        orders.setConsignee(addressBook.getConsignee());
        //将购物项转换为订单项
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, userId);
        //获取到购物车商品的集合
        List<ShoppingCart> shoppingCarts = shoppingCartService.list(wrapper);
        //创建订单详细集合准备接收
        List<OrderDetail> details = new ArrayList<>();
        //创建一个BigDecimal准备接收金额总和
        BigDecimal amount = new BigDecimal(0);
        for (ShoppingCart shoppingCart : shoppingCarts) {
            OrderDetail detail = new OrderDetail();
            //转换信息
            BeanUtils.copyProperties(shoppingCart, detail, "id");
            //将订单号字段填充
            detail.setOrderId(id);
            //获取到每个商品的单价及分数进行相乘并累加
            //商品的单价
            BigDecimal money = shoppingCart.getAmount();
            //商品的份数
            BigDecimal number = new BigDecimal(shoppingCart.getNumber());
            //让单价乘份数
            BigDecimal count = money.multiply(number);
            //将金额累加
            amount = amount.add(count);
            //将补好的对象放入集合
            details.add(detail);
        }
        //实收金额
        orders.setAmount(amount);
        //将订单基本信息存入
        this.save(orders);
        orderDetailService.saveBatch(details);
        //将购物车清空
        shoppingCartService.remove(wrapper);
    }
}
